package com.gen.utils.ngen;

import org.yaml.snakeyaml.Yaml;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Random;

public class ESNameGenUtils {

    private static final String name_file = "spain_names.yml";
    private static LinkedHashMap nameinfo;
    private static List<String> first_names;
    private static  List<String> femaleNames;
    private static  List<String> maleNames;

    static {
        try{
            Yaml yaml = new Yaml();
            nameinfo = yaml.load(JpNameGenUtils.class.getClassLoader().getResourceAsStream(name_file));
            first_names = (List<String>) nameinfo.get("first_name");
            femaleNames = (List<String>) ((LinkedHashMap) nameinfo.get("last_name")).get("female");
            maleNames = (List<String>) ((LinkedHashMap) nameinfo.get("last_name")).get("male");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static String genFirstName(){

        return first_names.get(getIndex(first_names.size()));
    }

    public static String genLastName(int sex){
        if(sex == 0 ){
            return femaleNames.get(getIndex(femaleNames.size()));
        }else {
            return maleNames.get(getIndex(maleNames.size()));
        }
    }

    /**
     * 随机名字生成
     * @return
     */
    public static String genName(int sex){
        String firstName = genFirstName();
        String lastName = genLastName(sex);
        return firstName+" "+lastName;
    }

    public static int getIndex(int len){
        int times = 0;
        int index = len;
        while(times<2){
            index = new Random().nextInt(index)+1;
            if(index == 1){
                break;
            }
            times++;
        }
        index--;
        return index;
    }
}
